package com.gopay.common.util;

import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;

/**
 * 数字证书工具类
 * 
 * @CaCertUtil.java
 * @author dak.wang
 * @2016年2月22日 下午2:55:22 www.gopay.com.cn Inc.All rights reserved.
 */
public class CaCertUtil {
    // 日志
    private static final Logger log = Logger.getLogger(CaCertUtil.class);

    /**
     * 生成密码控件的UUID随机数,并保存到session中
     * 
     * @param request
     * @return
     */
    public static synchronized String createUUID(HttpServletRequest request) {
        String uuid = createUUID();
        request.getSession().setAttribute(uuid, uuid);
        String s = (String)request.getSession().getAttribute(uuid);
        return uuid;
    }

    public static synchronized String createUUID() {
        // 生成随机数
        String uuid = UUID.randomUUID().toString().replaceAll("@", "");
        uuid = uuid.replaceAll("-", "0");
        if (uuid.length() >= 32) {
            uuid = uuid.substring(0, 32);
        } else {
            uuid += "0123456789abcdef0123456789abcdef".substring(0, 32 - uuid.length());
        }
        return uuid;
    }
    
    /**
     * 从session中获取数字证书UUID，获取之后删除该属性
     * 
     * @param request
     * @param nextId
     * @return
     */
    public static synchronized void removeSessionUUID(HttpServletRequest request, String uuid) {
        request.getSession().removeAttribute(uuid);
    }

}
